package com.changgou.footmark.service.impl;

import com.changgou.entity.Result;
import com.changgou.footmark.dao.FootmarkMapper;
import com.changgou.footmark.pojo.Footmark;
import com.changgou.footmark.service.FootmarkService;
import com.changgou.goods.feign.SkuFeign;
import com.changgou.goods.pojo.Sku;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;
import tk.mybatis.mapper.entity.Example;

import java.util.Date;
import java.util.List;

/****
 * @Author:admin
 * @Description:Footmark业务层接口实现类
 * @Date 2019/6/14 0:16
 *****/
@Service
public class FootmarkServiceImpl implements FootmarkService {

    @Autowired
    private FootmarkMapper footmarkMapper;
    @Autowired
    private SkuFeign skuFeign;


    /**
     * Footmark条件+分页查询
     * @param footmark 查询条件
     * @param page 页码
     * @param size 页大小
     * @return 分页结果
     */
    @Override
    public PageInfo<Footmark> findPage(Footmark footmark, int page, int size){
        //分页
        PageHelper.startPage(page,size);
        //搜索条件构建
        Example example = createExample(footmark);
        //执行搜索
        return new PageInfo<Footmark>(footmarkMapper.selectByExample(example));
    }

    /**
     * Footmark分页查询
     * @param page
     * @param size
     * @return
     */
    @Override
    public PageInfo<Footmark> findPage(int page, int size){
        //静态分页
        PageHelper.startPage(page,size);
        //分页查询
        return new PageInfo<Footmark>(footmarkMapper.selectAll());
    }

    /**
     * Footmark条件查询
     * @param footmark
     * @return
     */
    @Override
    public List<Footmark> findList(Footmark footmark){
        //构建查询条件
        Example example = createExample(footmark);
        //根据构建的条件查询数据
        return footmarkMapper.selectByExample(example);
    }


    /**
     * Footmark构建查询对象
     * @param footmark
     * @return
     */
    public Example createExample(Footmark footmark){
        Example example=new Example(Footmark.class);
        Example.Criteria criteria = example.createCriteria();
        if(footmark!=null){
            // 足迹id
            if(!StringUtils.isEmpty(footmark.getId())){
                    criteria.andEqualTo("id",footmark.getId());
            }
            // 浏览时间
            if(!StringUtils.isEmpty(footmark.getBrowseDate())){
                    criteria.andEqualTo("browseDate",footmark.getBrowseDate());
            }
            // 商品id
            if(!StringUtils.isEmpty(footmark.getSkuId())){
                    criteria.andEqualTo("skuId",footmark.getSkuId());
            }
            // 商品分类
            if(!StringUtils.isEmpty(footmark.getCategoryName())){
                    criteria.andEqualTo("categoryName",footmark.getCategoryName());
            }
            // 商品名称
            if(!StringUtils.isEmpty(footmark.getName())){
                    criteria.andLike("name","%"+footmark.getName()+"%");
            }
            // 单价
            if(!StringUtils.isEmpty(footmark.getPrice())){
                    criteria.andEqualTo("price",footmark.getPrice());
            }
            // 图片地址
            if(!StringUtils.isEmpty(footmark.getImage())){
                    criteria.andEqualTo("image",footmark.getImage());
            }
            // 用户名
            if(!StringUtils.isEmpty(footmark.getUserId())){
                    criteria.andEqualTo("userId",footmark.getUserId());
            }
        }
        return example;
    }

    /**
     * 删除
     * @param id
     */
    @Override
    public void delete(Integer id){
        footmarkMapper.deleteByPrimaryKey(id);
    }

    /**
     * 修改Footmark
     * @param footmark
     */
    @Override
    public void update(Footmark footmark){
        footmarkMapper.updateByPrimaryKey(footmark);
    }

    /**
     * 增加Footmark
     * @param footmark
     */
    @Override
    public void add(Footmark footmark){
        footmarkMapper.insert(footmark);
    }

    /**
     * 根据ID查询Footmark
     * @param id
     * @return
     */
    @Override
    public Footmark findById(Integer id){
        return  footmarkMapper.selectByPrimaryKey(id);
    }

    /**
     * 查询Footmark全部数据
     * @return
     */
    @Override
    public List<Footmark> findAll() {
        return footmarkMapper.selectAll();
    }

    /**
     * 根据用户名查询对应的全部浏览数据
     */
    @Override
    public List<Footmark> findByUsername(String username) {
        Footmark condition = new Footmark();
        condition.setUserId(username);
        return footmarkMapper.select(condition);
    }

    /**
     * 根据skuId增加浏览记录
     * @param id 商品skuId
     * @param username 用户id
     */
    @Override
    public void addBySkuId(Long id, String username) {
        // 0.先从浏览记录表中查询是否有浏览记录
        Footmark condition = new Footmark();
        condition.setSkuId(id);
        List<Footmark> footmarks = footmarkMapper.select(condition);
        // 0.1 如果存在，说明以前浏览过，则更新覆盖浏览日期即可
        if (footmarks != null && footmarks.size() > 0) {
            for (Footmark footmark : footmarks) {
                footmark.setBrowseDate(new Date());
                footmarkMapper.updateByPrimaryKey(footmark);
            }
            return;
        }
        // 0.2 如果不存在，则是第一次浏览，新增至数据库即可

        // 1.根据skuId调用feign查询商品的信息
        Result<Sku> skuResult = skuFeign.findById(id);
        Sku sku = skuResult.getData();

        // 2.补充数据并封装商品信息至Footmark对象
        Footmark footmark = new Footmark();
        footmark.setBrowseDate(new Date());
        footmark.setUserId(username);
        footmark.setSkuId(sku.getId());
        footmark.setCategoryName(sku.getCategoryName());
        footmark.setName(sku.getName());
        footmark.setPrice(sku.getPrice());
        footmark.setImage(sku.getImage());

        // 3.添加至数据库
        footmarkMapper.insertSelective(footmark);
    }
}
